package com.nodecollege.cloud.common.model;

import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.vo.MenuTreeVO;
import lombok.Data;

import java.util.List;

/**
 * @author LC
 * @date 2019/12/3 15:48
 */
@Data
public class LoginAdmin extends LoginInfo {
    /**
     * 拥有角色idList
     */
    private List<IdName> roleList;
    /**
     * 拥有的菜单树
     */
    private List<MenuTreeVO> menuTree;

    /**
     * 校验接口权限
     *
     * @param url
     */
    public void checkPower(String url) {
        boolean has = false;
        for (int i = 0; i < menuTree.size(); i++) {
            has = checkPower(menuTree.get(i), url);
            if (has) {
                return;
            }
        }
        throw new UpmsException("", "无访问该接口的权限！");
    }

    private boolean checkPower(MenuTreeVO menuTree, String url) {
        if (NCConstants.INT_2 <= menuTree.getMenuType()) {
            if ((menuTree.getApplicationName() + menuTree.getApiUrl()).equals(url)) {
                return true;
            }
        }
        if (menuTree.getChildren() != null) {
            for (int i = 0; i < menuTree.getChildren().size(); i++) {
                boolean has = checkPower(menuTree.getChildren().get(i), url);
                if (has) {
                    return true;
                }
            }
        }
        return false;
    }
}
